<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>礼服日历</title>
    <!-- 字体 -->
    <link rel="stylesheet" href="./css/font-awesome.min.css">
    <!-- 初始化CSS -->
    <link rel="stylesheet" href="./css/init.css">
    <!-- head -->
    <link rel="stylesheet" href="./css/header.css">
    <!-- loading -->
    <link rel="stylesheet" href="./css/qiein-loading.css">
    <!-- 公共 -->
    <link rel="stylesheet" href="./css/common.css">
    <!-- 本页面 -->
    <link rel="stylesheet" href="./css/dress-index.css">
    <!-- vue -->
    <script src="./js/vue.js"></script>
    <script src="./js/axios.js"></script>
</head>

<body>
    <div class="all-contain" id="dress-dispatch">
        <!-- 头部样式 -->
        <header class="qiein-head">
            <div class="qiein-logo">
                <img src="img/qiein-logo.png" alt="">
            </div>
            <!-- 头部导航 -->
            <div class="head-nav">
                预约日历<i class="fa fa-angle-down"></i>
                <div class="head-nav-ul-wrap">
                    <ul>
                       <li onclick="location.href='./operation-record.html'">预约日历</li>
                        <li onclick="location.href='./customer-manager.html'">销售客户管理</li>
                        <li onclick="location.href='./operation-record.html'">操作记录</li>
                    </ul>
                </div>
            </div>
            <!-- 头部搜索 -->
            <div class="head-search">
                <div>
                    <i class="fa fa-search"></i>
                    <input type="text">
                </div>
            </div>
            <!-- 头部提示 -->
            <div class="head-prompt">
                <i class="fa fa-bell"></i>
                <span>1</span>
            </div>
            <div class="login-user">
                <span><i class="fa fa-user"></i></span>
                <div>超级管理员</div>
            </div>
        </header>
        <!-- 头部样式 end-->
        <!-- 页面主体 -->
        <div class="content">
            <div class="content-title">预约日历</div>
            <!-- 主体左侧 -->
            <div class="main-left">
                <!-- 日历title -->
                <div class="cale-title">
                    <!-- 前一月 -->
                    <span @click="prevMonth()"><i class="fa fa-caret-left"></i></span>
                    <!-- 后一月 -->
                    <span @click="nextMonth()"><i class="fa fa-caret-right"></i></span>
                    <!-- 今天 -->
                    <span @click="clickNow()">今天</span>
                    <div class="fast-select-date">
                        <span @click="fastSelectShow=!fastSelectShow">
                            <span  v-text="timeYY+'年'+timeMM+'月'"></span><i class="fa fa-calendar"></i></span>
                        <table v-show="fastSelectShow" style="display: none">
                            <thead>
                                <tr>
                                    <td @click="prevYear()"><i class="fa fa-angle-left fa-fw"></i></td>
                                    <td v-text="timeYY" colspan="2"></td>
                                    <td @click="nextYear()"><i class="fa fa-angle-right fa-fw"></i></td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr v-for="y in 3">
                                    <td v-for="x in 4" v-text="((y-1)*4+x)" @click="turnMonth((y-1)*4+x)"></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <!-- 日历主体 -->
                <table class="cale-style">
                    <thead>
                        <tr>
                            <td>周日</td>
                            <td>周一</td>
                            <td>周二</td>
                            <td>周三</td>
                            <td>周四</td>
                            <td>周五</td>
                            <td>周六</td>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="y in 6">
                            <td v-for="x in 7" @click="turnDayPage(dateList[((y-1)*7+x)-1].daysStr)">
                                <div :class="{'cale-isThisDay':dateList[((y-1)*7+x)-1].isThisDay}">
                                    <!-- 档期天 -->
                                    <div :class="{'cale-notThisMonth':!dateList[((y-1)*7+x)-1].isThisMonth}" v-text="dateList[((y-1)*7+x)-1].date.getDate()"></div>
                                    <!-- 档期安排 -->
                                    <!-- <div class="cale-isOver"></div> -->
                                    <div :class="caleArrangeClass(((y-1)*7+x)-1)">
                                        <span v-text="renderPairs(((y-1)*7+x)-1)+'/'"></span><span v-text="renderMaxPairs(((y-1)*7+x)-1)"></span>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <!-- 主体左侧 end -->
            <!-- 主体右侧 -->
            <div class="main-right">
                <div class="caption">
                    <div>档期说明</div>
                    <ul>
                        <li>无人</li>
                        <li>档期有余</li>
                        <li>档期已满</li>
                    </ul>
                </div>
                <div class="record-style">
                    <div><span>最新操作记录</span><span  onclick="location.href='./operation-record.html'">查看更多</span></div>
                    <ul>
                        <li v-for="x in 30">刘曼 修改日期 为 2017.8.5</li>
                    </ul>
                </div>
            </div>
            <!-- 右侧结束 -->
        </div>
        <!-- 中间结束 -->
        <!-- loading -->
        <div class="qiein-loading" v-show="loadingShow" style="display: none">
            <div class="load-img"  style="display: inline-block;">
                <img src="img/loading.gif" alt="">
                <p>档期加载中...</p>
            </div>
        </div>
        <!-- Loading结束 -->
    </div>
</body>
<script>
"use strict";
var app = new Vue({
    el: '#dress-dispatch',
    data: {
        timeYY: '',
        timeMM: '',
        timeDD: '',
        fastSelectShow: false,
        loadingShow:false,
        dateList: []
    },
    created: function() {
        //页面创建时
        this.initDate();
        //获取后台数据
        this.getBackData();
    },
    mounted: function() {
        //页面载入完毕
    },
    updated: function() {
        //页面重新渲染
    },
    computed: {

    },
    methods: {
        initDate: function() {
            //初始化日历
            this.setNowDate();
            let monthNow = new Date(this.timeYY, this.timeMM - 1, 1);
            let fisrtDay = monthNow.getDay();
            //当前时间的字符串
            let thisTimeStr = this.getDateStr(new Date());
            //遍历42个TD
            for (let day = 0; day < 42; day++) {
                //生成当前表格的日期
                let _thisDay = new Date(this.timeYY, this.timeMM - 1, day + 1 - fisrtDay);
                let _thisDayStr = this.getDateStr(_thisDay);
                let dataTemp = {
                    id: 'day' + day,
                    //当前表格日期
                    date: _thisDay,
                    //当前表格的日期字符串
                    daysStr: _thisDayStr,
                    isThisDay: _thisDayStr == thisTimeStr ? true : false, //当前表格是否是今天
                    //当前表格是否是这个月
                    isThisMonth: _thisDay.getMonth() == (this.timeMM - 1) ? true : false,
                    data: {
                        pairs: Math.ceil((Math.random()*10))%2==0?10:24,
                        maxPairs: 24
                    }
                };
                //一定要用push，不然不会触发视图更新
                this.dateList.push(dataTemp);
            }
        },
        isLeap: function(year) {
            //是否为闰年
            return (year % 100 == 0 ? res = (year % 400 == 0 ? 1 : 0) : res = (year % 4 == 0 ? 1 : 0));
        },
        renderCalc: function() {
            //创建日历
            //console.log(this.timeYY + '' + this.timeMM);
            //判断当前月第一天是星期几
            let monthNow = new Date(this.timeYY, this.timeMM - 1, 1);
            let fisrtDay = monthNow.getDay();
            //当前时间的字符串
            let thisTimeStr = this.getDateStr(new Date());
            //遍历42个TD
            for (let day = 0; day < 42; day++) {
                //生成当前表格的日期
                let _thisDay = new Date(this.timeYY, this.timeMM - 1, day + 1 - fisrtDay);
                let _thisDayStr = this.getDateStr(_thisDay);
                this.dateList[day].id = 'day' + day;
                this.dateList[day].date = _thisDay;
                this.dateList[day].daysStr = _thisDayStr;
                //当前表格是否是今天;
                this.dateList[day].isThisDay= _thisDayStr == thisTimeStr ? true : false;
                this.dateList[day].isThisMonth = _thisDay.getMonth() == (this.timeMM - 1) ? true : false;
                //测试用
                this.dateList[day].data.pairs=Math.ceil((Math.random()*10))%2==0?10:24;
            }
            //获取后台数据
            this.getBackData();
        },
        setNowDate: function() {
            //获取今日时间
            let dateNow = new Date();
            this.timeYY = dateNow.getFullYear();
            this.timeMM = dateNow.getMonth() + 1;
            this.timeDD = dateNow.getDate();
        },
        clickNow: function() {
            this.setNowDate();
            this.renderCalc();
        },
        getMonthList: function() {
            //根据年份返回当前年 每个月天数 的数组
            return (new Array(31, 28 + this.isLeap(this.timeYY), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)); //各月份的总天数
        },
        getDateStr: function(date) {
            // 根据传入日期，返回其字符串
            var _year = date.getFullYear();
            var _month = date.getMonth() + 1; // 月从0开始计数
            var _d = date.getDate();
            _month = (_month > 9) ? ("" + _month) : ("0" + _month);
            _d = (_d > 9) ? ("" + _d) : ("0" + _d);
            return _year + _month + _d;
        },
        prevMonth: function() {
            // 上一月
            if (this.timeMM > 1) {
                this.timeMM -= 1;
            } else {
                this.timeYY -= 1;
                this.timeMM = 12;
            }
            this.renderCalc();
        },
        nextMonth: function() {
            // 下一月
            if (this.timeMM < 12) {
                this.timeMM += 1;
            } else {
                this.timeYY += 1;
                this.timeMM = 1;
            }
            this.renderCalc();
        },
        caleClass: function(index) {
            //计算需要绑定的class(td)
            return {
                'cale-notThisMonth': !(this.dateList[index].isThisMonth),
                'cale-isThisDay': this.dateList[index].isThisDay
            };
        },
        caleArrangeClass: function(index) {
            // this.dateList[index].data.
            let tempT;
            if (this.dateList[index].data.pairs==0) {
                return {'cale-none':true};
            }
            //console.log(this.dateList[index]);
            // if (this.dateList[index].data.maxPairs == 'undefined') {
            //     ttempT = false;
            // } else {
            //     tempT = this.dateList[index].data.pairs < this.dateList[index].data.maxPairs;
            // }
            tempT = this.dateList[index].data.pairs < this.dateList[index].data.maxPairs;
            return {
                'cale-hasMore': tempT,
                'cale-isOver': !tempT,
            };
        },
        prevYear: function() {
            // 上一年
            this.timeYY -= 1;
        //this.fastSelectShow = false;
            //this.renderCalc();
        },
        nextYear: function() {
            // 下一年
            this.timeYY += 1;
         //this.fastSelectShow = false;
            //this.renderCalc();
        },
        turnMonth: function(month) {
            //跳转到某月
            this.timeMM = month;
            this.fastSelectShow = false;
            this.renderCalc();
        },
        renderPairs: function(index) {
            // if (typeof(this.dateList[index].data.pairs) == 'undefined') {
            //     this.dateList[index].data.pairs = 0;
            // }
            // if(this.dateList[index].data.pairs<10) return '&nbsp;'+this.dateList[index].data.pairs;
            return this.dateList[index].data.pairs;
        },
        renderMaxPairs: function(index) {
            // if (typeof(this.dateList[index].data.maxPairs) == 'undefined') {
            //     this.dateList[index].data.maxPairs = 0;
            // }
            return this.dateList[index].data.maxPairs;
        },
        getBackData: function() {
            //获取后台数据
            let postData = {
                //开始时间
                startDate: this.dateList[0].daysStr,
                //结束时间
                endDate: this.dateList[41].daysStr
            };
            this.loadingShow=true;
            axios.post('../dispatch/getByDate', postData)
                .then((response) => {
                    let len = response.data.dispatches.length;
                    // for(let i=0;i<len;i++){
                    //     let temp=response.data['dispatches'][i];
                    //     this.dateList[temp.date]
                    // }
                    for (let i = 0; i < len; i++) {
                        //console.log(response.data['dispatches'][i].pairs);
                        if (this.dateList[i].daysStr == response.data['dispatches'][i].date) {
                            this.dateList[i].data = response.data['dispatches'][i];
                        }
                    }
                    // this.loadingShow=false;
                })
                .catch((err)=>{
                    setTimeout(()=>{this.loadingShow=false},500);
                });
        },
        turnDayPage:function(time){
            location.href="./day-record.html?time="+time;
        }
    }
});
</script>

</html>
